﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class BB03GB03Data : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";

        public int ThreadCount;
        private string OrderNum;
        DataTable tbWGSummary;
        public BB03GB03Data()
            : base()
        {
            
            tbWGSummary = new DataTable("WGGSUM");
            tbWGSummary.Columns.Add("ARTICLE", typeof(string));
            tbWGSummary.Columns.Add("WBBDPART", typeof(string));
            tbWGSummary.Columns.Add("FACTORY", typeof(string));
            tbWGSummary.Columns.Add("WBEWMTR", typeof(double));
            tbWGSummary.Columns.Add("PCNUM", typeof(int));
        }
        public BB03GB03Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string LOT;
            LOT = dwABPOS["LOT"].ToString() ;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                
                or1.CommandText = "SELECT trim(BMBEM) as BMBEM,TYPE,GABART,GABVAR,GSHANDLNR, "
                    + " GSGARNBEZ,GABFARBE,GSCBEZ,GPPARTIE,GPSPPART, "
                    + " GPFBPART,SUPPLIER,GPBESTTYP,GPBESTKRS,GPBESTLFD,GPBESTPOS,GPEINDAT "
                    + " FROM SIZINGLOT_PROD_PROCESS A  "
                    + " WHERE A.BMBEM LIKE '%" + LOT + "%'"
                    ; 
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "BB03GB03";
                    SetDefault(tableLocal);
                    byte[] b;
                    string value; 
                    string sTemp = ""; 
                    string Column = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "GSHANDLNR";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "GSGARNBEZ";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "GSCBEZ";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        GetSUPPLIER(dwTemp);
                    } 
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }        
        public DataSet GetSUPPLIER(DataRow dwUI)
        {
            string TableName = "SUPPLIER";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tableLocal = null;
            string GPBESTTYP, GPBESTKRS, GPBESTLFD, GPBESTPOS;
            GPBESTTYP = dwUI["GPBESTTYP"].ToString();
            GPBESTKRS = dwUI["GPBESTKRS"].ToString();
            GPBESTLFD = dwUI["GPBESTLFD"].ToString();
            GPBESTPOS = dwUI["GPBESTPOS"].ToString();
            try
            {
                if (GPBESTTYP == "V" )
                {
                    orlReadCommand = OracleDB.CreateCommand();
                    orlReadCommand.CommandText = "SELECT DISTINCT TRIM(B.LSNAME1) AS SUPPLIER FROM ABFOLGE A,LIEFDAT B "
                        + " WHERE A.ABFMANDANT = B.LSMANDANT AND A.ABFLIEFNR = B.LSLIEFNR "
                        + " AND A.ABFAUFKRS = '" + GPBESTKRS + "' AND A.ABFAUFLFD = '" + GPBESTLFD + "' "
                        + " AND A.ABFAUFPOS = '" + GPBESTPOS + "' "
                        ;

                    setResult = Connect2Proxy(orlReadCommand);
                    if (setResult.Tables.Count > 0)
                    {
                        setResult.Tables[0].TableName = TableName;
                        tableLocal = setResult.Tables[0];
                        tableLocal.TableName = TableName;
                        byte[] b;
                        string value; 
                        string sTemp = ""; 
                        string Column = "";
                        foreach (DataRow dwTemp in tableLocal.Rows)
                        {

                            Column = "SUPPLIER";
                            #region
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dwTemp[Column] = value;
                            #endregion

                            dwUI["SUPPLIER"] = value;

                        }

                    }
                }
                if (GPBESTTYP == " ")
                {
                    orlReadCommand = OracleDB.CreateCommand();
                    orlReadCommand.CommandText = "SELECT DISTINCT TRIM(B.LSNAME1) AS SUPPLIER FROM EKPOS A,LIEFDAT B "
                        + " WHERE A.EKPMANDANT = B.LSMANDANT AND A.EKPLIEFNR = B.LSLIEFNR "
                        + " AND A.EKPSA = 'PO' AND A.EKPAUFKRS = '" + GPBESTKRS + "' AND A.EKPAUFLFD = '" + GPBESTLFD + "'  "
                        + " AND A.EKPAUFPOS = '" + GPBESTPOS + "' "
                        + " UNION ALL "
                        + " SELECT DISTINCT TRIM(B.LSNAME1) AS SUPPLIER FROM EKSTPOS A,LIEFDAT B "
                        + " WHERE A.EKSTPMANDANT = B.LSMANDANT AND A.EKSTPLIEFNR = B.LSLIEFNR "
                        + " AND A.EKSTPAUFKRS = '" + GPBESTKRS + "' AND A.EKSTPAUFLFD = '" + GPBESTLFD + "'  "
                        + " AND A.EKSTPAUFPOS = '" + GPBESTPOS + "' " 
                        ;

                    setResult = Connect2Proxy(orlReadCommand);
                    if (setResult.Tables.Count > 0)
                    {
                        setResult.Tables[0].TableName = TableName;
                        tableLocal = setResult.Tables[0];
                        tableLocal.TableName = TableName;

                        byte[] b;
                        string value; 
                        string sTemp = ""; 
                        string Column = "";
                        foreach (DataRow dwTemp in tableLocal.Rows)
                        {
                        
                            Column = "SUPPLIER";
                            #region
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dwTemp[Column] = value;
                            #endregion
                            
                            dwUI["SUPPLIER"] = value;


                         }

                    }
                }

                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}